package com.freesky.biz.icbc.service;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.freesky.biz.icbc.config.IcbcConfiguration;
import com.freesky.biz.icbc.model.IcbcPointQryRequestV1;
import com.freesky.biz.icbc.model.IcbcPointQryResponseV1;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.redis.RedisCache;
import com.icbc.api.DefaultIcbcClient;
import com.icbc.api.IcbcConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 积分兑换项目
 */
@Service
public class IcbcPointService {

    private static Logger logger = LoggerFactory.getLogger(IcbcPointService.class);

    @Autowired
    @Qualifier("icbcConfiguration")
    private IcbcConfiguration icbcConfiguration;

    @Autowired
    private RedisCache redisCache;

    private String PREFIX_URL = "https://gw.open.icbc.com.cn/api/";
    private String MCT_CODE = "0001";

    //private String PREFIX_URL = "https://apipcs3.dccnet.com.cn/api/";

    //查询
    public AjaxResult pointQuery(String mtiCode,String mobile) {
        try {
            DefaultIcbcClient client = new DefaultIcbcClient(icbcConfiguration.getAppId(),
                    IcbcConstants.SIGN_TYPE_RSA2,
                    icbcConfiguration.getPrivateKey(),
                    icbcConfiguration.getPubGatewayKey());

            IcbcPointQryRequestV1 request = new IcbcPointQryRequestV1();

            request.setServiceUrl(PREFIX_URL + "fjfh/mybank/account/jfzddh/equityqry/V1");

            IcbcPointQryRequestV1.IcbcPointQryRequestV1Biz bizContent = new IcbcPointQryRequestV1.IcbcPointQryRequestV1Biz();

            bizContent.setMti("1201");
            bizContent.setMctcode(mtiCode);
            bizContent.setTrantime(DateUtil.format(new Date(),"yyyyMMddHHmmss"));
            bizContent.setMobileNo(mobile);
            request.setBizContent(bizContent);
            logger.info("【pointQuery req,mobile:"+mobile+"，"+icbcConfiguration.getAppId()+"】:"+ JSON.toJSONString(bizContent));
            IcbcPointQryResponseV1 response = new IcbcPointQryResponseV1();
            response = client.execute(request);
            logger.info("【pointQuery resp,mobile:"+mobile+"】:"+JSON.toJSONString(response));
            if(response.isSuccess()) {
                if(response.getRetCode().equalsIgnoreCase("00") || response.getRetCode().equalsIgnoreCase("03")){
                    return AjaxResult.success("成功",response);
                } else {
                    return AjaxResult.error(response.getReturnMsg());
                }
            } else {
                return AjaxResult.error(response.getReturnMsg());
            }
        } catch (Exception ioe) {
            logger.error("【pointQuery,exception mobile:"+mobile+"】:"+ioe.getMessage());
            ioe.printStackTrace();
            return AjaxResult.error("操作失败，系统异常");
        }
    }

}
